package com.aaa.common.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.entity.DictData;
import com.aaa.common.entity.DictType;
import com.aaa.common.mapper.DictDataMapper;
import com.aaa.common.mapper.DictTypeMapper;
import com.aaa.common.service.DictDataService;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 字典数据表(DictData)表服务实现类
 *
 * @author makejava
 * @since 2020-11-16 21:56:01
 */
@Service("dictDataService")
public class DictDataServiceImpl implements DictDataService {
    @Resource
    private DictDataMapper dictDataMapper;
    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param dictCode 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer dictCode) {
        if (dictCode == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictDataMapper.queryById(dictCode));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public Result queryAllByLimit(Integer offset, Integer limit) {
        if (offset == null || limit == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictDataMapper.queryAllByLimit(offset, limit));
    }

    /**
     * 新增数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(DictData dictData) {
        if (dictData == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        String dictName = dictData.getDictName();
        DictType dictType = new DictType();
        dictType.setDictName(dictName);
        dictType.setCreateTime(new Date());
        dictType.setDictType(dictData.getDictType());
        this.dictTypeMapper.insert(dictType);
        this.dictDataMapper.insert(dictData);
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), dictData);
    }

    /**
     * 修改数据
     *
     * @param dictData 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(DictData dictData) {
        if (dictData == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        this.dictDataMapper.update(dictData);
        Integer dictCode = (Integer) dictData.getDictCode();
        return this.queryById(dictCode);
    }

    /**
     * 通过主键删除数据
     *
     * @param dictCode 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer dictCode) {
        if (dictCode == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictDataMapper.deleteById(dictCode) > 0);
    }
    /**
     * 查询指定行数据
     *
     * @param pageNo   查询起始位置
     * @param pageSize 查询条数
     * @param dictData     查询条数
     * @return Result ---- Result.getData() 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, DictData dictData) {
        if (pageNo == 0 || pageSize == 0) {
            throw  new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<DictData> pageInfo = new PageInfo<>(dictDataMapper.queryAll(dictData));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }
    /**
     * 初始化所有字典类型
     * 为字典数据添加做准备
     */
    @Override
    public Result queryAllDictType() {
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictDataMapper.queryAllDictType());
    }
}